package org.example.ychdp.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.ychdp.dao.UserDao;
import org.example.ychdp.entity.User;
import org.example.ychdp.entity.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public User selectByPrimaryKey(Integer userid) {
        return userDao.selectByPrimaryKey(userid);
    }
    public User selectUserByName(User user) {
        User user1 = userDao.selectUserByName(user.getUsername());
        if (user1!=null && user1.getPassword().equals(user.getPassword())) {
            return user1;
        }
        return userDao.selectUserByName(user.getUsername());
    }


    public boolean checkUsernameExists(String username) {
        // 调用UserMapper中的方法查询数据库中是否存在该用户名的用户
        User users = userDao.selectUserByName(username);
        return users!= null;
    }


    public PageInfo<User> searchUser(Integer page, String nickname, String number) {
        PageHelper.startPage(page, 10);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameLike("%" + nickname + "%");
        criteria.andPhonenumberLike("%" + number + "%");
        List<User> userList = userDao.selectByExample(userExample);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    public List<User> selectAllUser() {
        return userDao.selectAllUser();
    }

    public int deleteUserById(Integer userid) {
        return userDao.deleteByPrimaryKey(userid);
    }

    public int insertUser(User user) {
        return userDao.insertSelective(user);
    }

    public User getUserById(Integer id) {
        return userDao.selectByPrimaryKey(id);
    }

    public int updateUser(User user) {
        return userDao.updateByPrimaryKey(user);
    }

    public int update(User user) {
        return userDao.updateByPrimaryKeySelective(user);
    }
}
